package gggameserver.dbaccess;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;

import gggameserver.GGGameServerGameResult;
import gggameserver.compiler.GGSolutionData;
import gggameserver.dbaccess.data.GGDBAGameInfo;
import gggameserver.exceptions.GGNoDataFoundException;

public class GGTestDBAccess implements GGDBAccess {

	private static int game_id = 0;
	
	private PrintWriter log;
	
	public GGTestDBAccess() throws FileNotFoundException {
		log = new PrintWriter(new FileOutputStream("test/log.txt"), true);
	}
	
	@Override
	public GGDBAGameInfo getNewGame() {
		synchronized (this.getClass()) {
		    if (0 == game_id) return new GGDBAGameInfo(game_id++, 0, 0); //draw

			//if (0 == game_id) return new GGDBAGameInfo(game_id++, 409, 464);
			
			
			// if (0 == game_id) return new GGDBAGameInfo(game_id++, 409, 466); //draw
			// if (0 == game_id) return new GGDBAGameInfo(game_id++, 409, 421); //draw
			//if (0 == game_id) return new GGDBAGameInfo(game_id++, 409, 467); //467
			
			//if (0 == game_id) return new GGDBAGameInfo(game_id++, 466, 467); //466
			//if (0 == game_id) return new GGDBAGameInfo(game_id++, 466, 421); // null

			//if (0 == game_id) return new GGDBAGameInfo(game_id++, 421, 467); // null

			//if (0 == game_id) return new GGDBAGameInfo(game_id++, 467, 421);
			
			//if (0 == game_id) return new GGDBAGameInfo(game_id++, 467, 466); // 466
			//if (0 == game_id) return new GGDBAGameInfo(game_id++, 421, 466); // null

			//if (0 == game_id) return new GGDBAGameInfo(game_id++, 467, 409); // 467
			//if (0 == game_id) return new GGDBAGameInfo(game_id++, 409, 421); 
			// if (0 == game_id) return new GGDBAGameInfo(game_id++, 466, 409); //draw

			//return new GGDBAGameInfo(game_id++, 4, 4);
				
			return null;
		}
	}

	@Override
	/**
	 * Filename format: <id>-<language=java>[---<comments>].zip
	 * (non-Javadoc)
	 * @see gggameserver.dbaccess.GGDBAccess#getSolutionData(long)
	 */
	public GGSolutionData getSolutionData(long id) {
		File f = new File("test");
		String prefix = id + "-";
		String suffix = ".zip";
		
		for (File file : f.listFiles()) {
			String filename = file.getName();
			if (filename.startsWith(prefix) && filename.endsWith(suffix)) {
				String language = filename.substring(prefix.length());
				int pos = language.indexOf("---");
				if (pos >= 0) {
					language = language.substring(0, pos);
				} else {
					language = language.substring(0, language.length() - suffix.length());
				}
				
				try {
System.out.println("# getSolutionData(" + id + ") -> " + language + " " + file.getAbsolutePath());
					GGSolutionData sdata = new GGSolutionData(language, file.getAbsolutePath(), id, id);
					return sdata;
				} catch (GGNoDataFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
			}
		}
		
System.out.println("Couldn't find 'test/" + prefix + "*" + suffix + "'");
		return null;
	}

	@Override
	public GGSolutionData getSolutionDataByProgramId(long prog_id) {
		return getSolutionData(prog_id);
	}

	@Override
	public boolean submitGameResults(long p_game_id, long prog_1, long prog_2,
			Long prog_winner, GGGameServerGameResult ladderGame) {
		log.println("=== Game result ===");
		log.println("game_id = " + p_game_id);
		log.println("prog_1 = " + prog_1);
		log.println("prog_2 = " + prog_2);
		log.println("prog_winner = " + prog_winner);
		log.println("log len = " + ladderGame.getGameLogData().length + "");
		
		try {
			FileOutputStream file = new FileOutputStream("test/log_" + p_game_id + ".zip");
			file.write(ladderGame.getGameLogData());
			file.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Couldn't save log...");
		}
		
		return true;
	}

	@Override
	public boolean setCompileResult(long prog_id, char result, String logdata) {
		this.log.println("=== Compile result ===");
		this.log.println("prog_id = " + prog_id);
		this.log.println("result = '" + result + "'");
		this.log.println("log = '" + logdata + "'");
		return true;
	}

	@Override
	public void deleteAvailableGame(long p_game_id) {
		this.log.println("=== Delete game ===");
		this.log.println("game_id = " + p_game_id);
	}

}
